package cn.echo.consul.manager.controller;

import cn.echo.consul.manager.annotation.RequestPermissions;
import cn.echo.consul.manager.encrypt.Md5;
import cn.echo.consul.manager.entity.Token;
import cn.echo.consul.manager.entity.UserInfo;
import cn.echo.consul.manager.service.UserInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.util.List;

import static cn.echo.consul.manager.constant.HeaderFields.AUTH_USER_ID;
import static cn.echo.consul.manager.constant.HeaderFields.UserAgent;
import static cn.echo.consul.manager.enums.PermissionRole.*;
import static cn.echo.consul.manager.enums.PermissionRole.VIEWER;

/**
 * 用户信息管理
 * @author lonyee
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/user")
public class UserInfoController {

    final UserInfoService userInfoService;

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Token login(@RequestHeader(UserAgent)String userAgent,
                       String userId, String secret) {
        String fingerprint = Md5.getMD5(userAgent);
        return userInfoService.login(userId, secret, fingerprint);
    }

    /**
     * 获取用户的信息列表
     */
    @RequestPermissions({ ADMIN })
    @GetMapping("/list")
    public List<UserInfo> getUserList() {
        return userInfoService.getUserList();
    }

    /**
     * 添加用户
     */
    @RequestPermissions({ ADMIN })
    @PostMapping("/append")
    public void append(String userId, String userName, String secret, Boolean isAdmin,
                       @RequestAttribute(AUTH_USER_ID) String creatorId) {
        userInfoService.register(userId, userName, secret, isAdmin, creatorId);
    }

    /**
     * 修改用户信息
     */
    @RequestPermissions({ ADMIN })
    @PostMapping("/modify")
    public void modify(String userId, String userName, Boolean isAdmin, Boolean usable,
                       @RequestAttribute(AUTH_USER_ID) String updaterId) {
        userInfoService.modify(userId, userName, isAdmin, usable, updaterId);
    }

    /**
     * 修改用户密码
     */
    @RequestPermissions({ ADMIN })
    @PostMapping("/modify/secret")
    public void modifySecret(String userId, String newSecret, String reSecret,
                             @RequestAttribute(AUTH_USER_ID) String updaterId) {
        userInfoService.modifySecret(userId, newSecret, reSecret, updaterId);
    }

    /**
     * 修改我的密码
     */
    @PostMapping("/mine/modify/secret")
    public void modifyMineSecret(@RequestAttribute(AUTH_USER_ID) String userId,
                                 String oldSecret, String newSecret, String reSecret) {
        userInfoService.modifyMineSecret(userId, oldSecret, newSecret, reSecret);
    }


    /***** 初始化系统管理员账户--登录设置新管理员后可禁用 **Start****/
    @Value("${manager.init.admin.userId:consul}")
    private String userId;
    @Value("${manager.init.admin.secret:lon10086}")
    private String secret;

    @PostConstruct
    public void initAdmin() {
        try {
            userInfoService.register(userId, userId, secret, Boolean.TRUE, userId);
            log.info("管理员账号注册成功，Account: {}, Secret: {}", userId, secret);
        } catch (Exception e) {
            log.info("管理员账号已存在，Account: {}", userId);
        }
    }
    /***** 初始化系统管理员账户--登录设置新管理员后可禁用 **End****/
}
